<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter&#160;35.&#160;Boost.Unordered</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part&#160;I.&#160;The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="boost_units/TODO.html" title="TODO">
<link rel="next" href="unordered/buckets.html" title="The Data Structure">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_units/TODO.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unordered/buckets.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="unordered"></a>Chapter&#160;35.&#160;Boost.Unordered</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Daniel</span> <span class="surname">James</span>
</h3></div></div>
<div><p class="copyright">Copyright &#169; 2003, 2004 Jeremy B. Maitin-Shepard</p></div>
<div><p class="copyright">Copyright &#169; 2005-2008 Daniel
      James</p></div>
<div><div class="legalnotice">
<a name="unordered.legal"></a><p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="unordered.html#unordered.intro">Introduction</a></span></dt>
<dt><span class="section"><a href="unordered/buckets.html">The Data Structure</a></span></dt>
<dt><span class="section"><a href="unordered/hash_equality.html">Equality Predicates and Hash Functions</a></span></dt>
<dt><span class="section"><a href="unordered/comparison.html">Comparison with Associative Containers</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html">C++11 Compliance</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.move">Move emulation</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.allocator_compliance">Use of allocators</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.pairs">Pairs</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.misc">Miscellaneous</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="unordered/rationale.html">Implementation Rationale</a></span></dt>
<dt><span class="section"><a href="unordered/changes.html">Change Log</a></span></dt>
<dt><span class="section"><a href="unordered/reference.html">Reference</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="unordered/reference.html#header.boost.unordered_set_hpp">Header &lt;boost/unordered_set.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="unordered/reference.html#header.boost.unordered_map_hpp">Header &lt;boost/unordered_map.hpp&gt;</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="unordered/bibliography.html">Bibliography</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="unordered.intro"></a><a class="link" href="unordered.html#unordered.intro" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
      For accessing data based on key lookup, the C++ standard library offers <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span></code>,
      <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span></code>, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">multiset</span></code>
      and <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">multimap</span></code>. These are generally implemented
      using balanced binary trees so that lookup time has logarithmic complexity.
      That is generally okay, but in many cases a <a href="http://en.wikipedia.org/wiki/Hash_table" target="_top">hash
      table</a> can perform better, as accessing data has constant complexity,
      on average. The worst case complexity is linear, but that occurs rarely and
      with some care, can be avoided.
    </p>
<p>
      Also, the existing containers require a 'less than' comparison object to order
      their elements. For some data types this is impossible to implement or isn't
      practical. In contrast, a hash table only needs an equality function and a
      hash function for the key.
    </p>
<p>
      With this in mind, unordered associative containers were added to the C++ standard.
      This is an implementation of the containers described in C++11, with some
      <a class="link" href="unordered/compliance.html" title="C++11 Compliance">deviations from the standard</a> in
      order to work with non-C++11 compilers and libraries.
    </p>
<p>
      <code class="computeroutput"><span class="identifier">unordered_set</span></code> and <code class="computeroutput"><span class="identifier">unordered_multiset</span></code> are defined in the header
      &lt;<code class="computeroutput"><a class="link" href="unordered/reference.html#header.boost.unordered_set_hpp" title="Header &lt;boost/unordered_set.hpp&gt;">boost/unordered_set.hpp</a></code>&gt;
    </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span>
        <span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span>
        <span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_set.html" title="Class template unordered_set">unordered_set</a></code><span class="special">;</span>

    <span class="keyword">template</span><span class="special">&lt;</span>
        <span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span>
        <span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_multiset.html" title="Class template unordered_multiset">unordered_multiset</a></code><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      <code class="computeroutput"><span class="identifier">unordered_map</span></code> and <code class="computeroutput"><span class="identifier">unordered_multimap</span></code> are defined in the header
      &lt;<code class="computeroutput"><a class="link" href="unordered/reference.html#header.boost.unordered_map_hpp" title="Header &lt;boost/unordered_map.hpp&gt;">boost/unordered_map.hpp</a></code>&gt;
    </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span>
        <span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Mapped</span><span class="special">,</span>
        <span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_map.html" title="Class template unordered_map">unordered_map</a></code><span class="special">;</span>

    <span class="keyword">template</span><span class="special">&lt;</span>
        <span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Mapped</span><span class="special">,</span>
        <span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;,</span>
        <span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">Key</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_multimap.html" title="Class template unordered_multimap">unordered_multimap</a></code><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      When using Boost.TR1, these classes are included from <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">unordered_set</span><span class="special">&gt;</span></code>
      and <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">unordered_map</span><span class="special">&gt;</span></code>, with the classes added to the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tr1</span></code>
      namespace.
    </p>
<p>
      The containers are used in a similar manner to the normal associative containers:
    </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unordered_map</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">map</span><span class="special">;</span>
<span class="identifier">map</span> <span class="identifier">x</span><span class="special">;</span>
<span class="identifier">x</span><span class="special">[</span><span class="string">"one"</span><span class="special">]</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
<span class="identifier">x</span><span class="special">[</span><span class="string">"two"</span><span class="special">]</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="identifier">x</span><span class="special">[</span><span class="string">"three"</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span>

<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"one"</span><span class="special">)</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="string">"missing"</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
</pre>
<p>
    </p>
<p>
      But since the elements aren't ordered, the output of:
    </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">BOOST_FOREACH</span><span class="special">(</span><span class="identifier">map</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">&lt;&lt;</span><span class="identifier">i</span><span class="special">.</span><span class="identifier">first</span><span class="special">&lt;&lt;</span><span class="string">","</span><span class="special">&lt;&lt;</span><span class="identifier">i</span><span class="special">.</span><span class="identifier">second</span><span class="special">&lt;&lt;</span><span class="string">"\n"</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
    </p>
<p>
      can be in any order. For example, it might be:
    </p>
<pre class="programlisting"><span class="identifier">two</span><span class="special">,</span><span class="number">2</span>
<span class="identifier">one</span><span class="special">,</span><span class="number">1</span>
<span class="identifier">three</span><span class="special">,</span><span class="number">3</span>
</pre>
<p>
      To store an object in an unordered associative container requires both an key
      equality function and a hash function. The default function objects in the
      standard containers support a few basic types including integer types, floating
      point types, pointer types, and the standard strings. Since Boost.Unordered
      uses <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code> it also supports
      some other types, including standard containers. To use any types not supported
      by these methods you have to <a class="link" href="hash/custom.html" title="Extending boost::hash for a custom data type">extend Boost.Hash
      to support the type</a> or use your own custom equality predicates and hash
      functions. See the <a class="link" href="unordered/hash_equality.html" title="Equality Predicates and Hash Functions">Equality Predicates
      and Hash Functions</a> section for more details.
    </p>
<p>
      There are other differences, which are listed in the <a class="link" href="unordered/comparison.html" title="Comparison with Associative Containers">Comparison
      with Associative Containers</a> section.
    </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: January 31, 2013 at 01:32:06 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_units/TODO.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unordered/buckets.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
